package com.groupbuying.store.controller.forManage;

import com.groupbuying.store.connector.AdminFeign;
import com.groupbuying.store.connector.SysconfigFeign;
import com.groupbuying.store.constants.CommonConstants;
import com.groupbuying.store.constants.StoreConstants;
import com.groupbuying.store.domain.StoreAccountDO;
import com.groupbuying.store.dto.forManage.AccountDTO;
import com.groupbuying.store.dto.forManage.do2dto.StoreAccountConvert;
import com.groupbuying.store.service.StoreAccountService;
import com.groupbuying.store.service.forManage.StoreRoleService;
import com.groupbuying.store.utils.PageUtils;
import com.groupbuying.store.utils.Query;
import com.groupbuying.store.utils.R;
import com.groupbuying.store.utils.apiresult.ApiResult;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("管理后台-服务商管理")
@RequestMapping("/accountMag")
@RestController
public class StoreAccountManageController extends BaseController {
    @Autowired
    StoreAccountService storeAccountService;

    @Autowired
    StoreRoleService roleService;

    @Autowired
    SysconfigFeign sysconfigFeign;
    @Autowired
    AdminFeign adminFeign;
    @Autowired
    StoreAccountConvert storeAccountConvert;
    /**
     * 从管理后台分页获取主账号
     * @param params
     * @return
     */
    private static final Logger logger = LoggerFactory.getLogger(StoreAccountManageController.class);
    @GetMapping("/getMainList")
    public R listMainByPage(@RequestParam Map<String, Object> params) {
        params.put("delFlag",CommonConstants.COMMON_FLAG_UNDEL);
        params.put("providerType",StoreConstants.STORE_MAIN_ACCOUNT.toString());
        Query query = new Query(params);
        List<AccountDTO> userDTOS = storeAccountConvert.dos2dto((storeAccountService.getStoreAndAgentName(query)));
        logger.info("返回Account为 userDTOS:{}",userDTOS);

        int total = storeAccountService.countForMinList(query);
        PageUtils pageUtil = new PageUtils(userDTOS, total);
        Map<String,Object> param = new HashMap<>();
        param.put("page",pageUtil);

        return R.ok(param);
    }

    @PostMapping("/updateFreezeStatus")
    public R updateFreezeStatus( @RequestBody Map<String,Object> map){
        System.out.println("storeAccountDO:{}"+map);
        return storeAccountService.updateFreezeStatus(map);

    }

    /**
     * 从管理后台增加用户
     * @param storeAccountDO
     * @return
     */
    @PostMapping("/addAdmin")
    public ApiResult save(@RequestBody StoreAccountDO storeAccountDO) {
        return storeAccountService.saveForAdmin(storeAccountDO);
    }

}
